package com.kifas.utils;

import java.util.List;

import com.kifas.entity.StokOpnameEntity;

public class StokOpnameMailGenerator {
	
	private final static String HEADER = "ID_BARANG,NAMA,GUDANG,JUMLAH";
	private final static String NEWLINE = "\n";
	private final static String SEPARATOR = ",";
	
	private List<StokOpnameEntity> stokOpnames;

	public StokOpnameMailGenerator(List<StokOpnameEntity> stokOpnames) {
		this.stokOpnames = stokOpnames;
	}
	
	public String getMessage() {
		StringBuilder builder = new StringBuilder();
		builder.append(HEADER).append(NEWLINE);
		Long totalQty = 0L;
		Long idBarang = null;
		Long idGudang = null;
                String kdBarang = "";
                String namaBarang = "";
		String namaGudang = null;
		boolean hasLoop = false;
		for(StokOpnameEntity stokOpname : stokOpnames) {
			if (hasLoop && stokOpname.getBarang().getIdBarang() != idBarang
					|| stokOpname.getGudang().getIdGudang() != idGudang) {
				builder
					.append(stokOpname.getBarang().getKdBarang())
					.append(SEPARATOR)
					.append(stokOpname.getBarang().getNama())
					.append(SEPARATOR)
					.append(namaGudang)
					.append(SEPARATOR)
					.append(totalQty)
					.append(NEWLINE);
				totalQty = 0L;
			}
			idBarang = stokOpname.getBarang().getIdBarang();
			idGudang = stokOpname.getGudang().getIdGudang();
			namaGudang = stokOpname.getGudang().getNamaGudang();
                        namaBarang = stokOpname.getBarang().getNama();
                        kdBarang = stokOpname.getBarang().getKdBarang();
			totalQty += stokOpname.getQty();
			hasLoop = true;
		}
		if (hasLoop) {
			builder
					.append(kdBarang)
					.append(SEPARATOR)
					.append(namaBarang)
					.append(SEPARATOR)
					.append(namaGudang)
					.append(SEPARATOR)
					.append(totalQty)
					.append(NEWLINE);
		totalQty = 0L;
		}
		return builder.toString();
	}
	
	
}
